var ns;
Vue.component('tree-item', {
    template: '#item-template',
    props: {
        item: {}
    },
    data: function () {
        return {
            isOpen: true,
            selecttypeid: app.TypeID
        }
    },
    computed: {
        isFolder: function () {
            return this.item.children &&
                this.item.children.length
        }
    },
    methods: {
        toggle: function (item) {
            if (this.isFolder) {
                this.isOpen = !this.isOpen
            } else {
                this.choose_type(item);
            }
        },
        choose_type: function (item) {
            var that = this;
            app.TypeID = item.ID;
            app.TypeName = item.text;
            setTimeout(function () {
                app.is_type_list = false;
            }, 100)
        },
    }
})
var app = new Vue({
    el: '#app',
    data: {
        list: [],
        ServiceType: 1, //1-客服工单 2-投诉建议 3-品控管理 4-审批中心
        PublishType: 1, //1-派单 2-工单转移 3-工单协同 4-重派 5-审批 6-抄送
        source: '',//来源 chooseuser:选择负责人
        ID: 0,
        select_id_list: [],
        is_xietong: true,
        is_type_list: false,
        is_tag_list: false,
        is_category_list: false,
        typelist: [],
        TypeID: 0,
        TypeName: '请选择',
        taglist: [],
        TagID: 0,
        TagName: '请选择',
        categorylist: [],
        CategoryID: 0,
        CategoryName: '请选择',
        showchoose: false,
        showsearch: false,//遮罩层
        chooseList: [],
        form: {
            keywords: '',
            pageindex: 0,
            pagesize: 10,
        },
        zxunchaID:0, //装修巡查id
        zxtype:0, //装修巡查类型
        personid:0,
        chooseType:''//yuangongchengfa--员工惩罚
    },
    methods: {
        show_morehistory: function (item) {
            var that = this;
            item.isopen = !item.isopen;
        },
        doHideSearch: function () {
            var that = this;
            that.showchoose = false;
            that.showsearch = false;
        },
        showChooseAll: function () {
            var that = this;
            that.showchoose = !that.showchoose;
            that.showsearch = !that.showsearch;//底部阴影
            
        },
        doChoosUser: function (item) {
            var that = this;
            //单选
            if (that.source == 'chooseuser') {
                that.chooseList = [];
                that.chooseList.push(item);
                that.personid=item.UserID
            }
            //多选
            else {
                item.isselected = !item.isselected;

                if (item.isselected) {
                    that.chooseList.push(item);
                }
                else {
                    const index = that.chooseList.findIndex(p => p.ID === item.ID && p.TypeID === item.TypeID);
                    that.chooseList.splice(index, 1)
                }

                const new_list = [...new Set(that.chooseList)];// 去重
                that.chooseList = new_list;
            }
        },
        get_data: function () {
            var that = this;
            if(that.chooseType=='yuangong'||that.chooseType=='yuangongchengfa'){
                that.get_yuangong();
            }else{
                that.get_user();
            }
        },
        get_user:function(){
            var that=this;
            var actionValue = "getuserlist";
            var options = {
                action: actionValue,
                KeyWords: that.form.keywords,
            };
            ns.post(options, function (succeed, data, err) {
                    if (data != null) {
                        if (data.status) {
                            that.list = data.data;
                        } else {
                            api.toast({
                                msg: err,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    }
            },{
                toast: true
            });
        },
        get_yuangong:function(){
            var that = this; 
            var actionValue = "getstafuserlist";
            var options = {
                action: actionValue,
                KeyWords: that.form.keywords,
                StaffStatus:0,
                pagesize : that.form.pagesize,
                pageindex : that.form.pageindex,

            };
            ns.post(options, function (succeed, data, err) {
                    if (succeed) {
                        let returnlist = data.list;
                        returnlist = returnlist.map(res => {
                            res.UserID = res.ID;
                            res.UserName = res.StaffName;
                            res.isselected=false;
                            return res
                        })
                        that.list=returnlist;
                    } else {
                        api.toast({
                            msg: err,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }

            },{
                toast: true
            });
        },
        //装修巡查转交
        tozhuanjiao:function(){
            var that = this;
            var options = {
                ID: that.zxunchaID,
                UserID:that.personid,
                action: "taskcheckuserchange",
            };
            ns.post(options, function (succeed, data, err) {
                    if (succeed) {
                        api.toast({
                            msg: '转交成功',
                            duration: 2000,
                            location: 'bottom'
                        });
                        api.sendEvent({
                            name: 'zhuanjiao_updata',
                        });
                        setTimeout(function(){
                            api.closeWin();
                        }, 500);
                        
                    }
                    else {
                        api.toast({
                            msg: err,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
            },{
                toast: true
            });
        },
        clear_searchVal: function () {
            var that = this;
            document.getElementById("search-input").value = '';
        },
        do_search: function () {
            this.get_data();
        },
        do_select_publishtype: function (type) {
            this.PublishType = type;
        },
        choose_tag: function (item) {
            var that = this;
            that.TagID = item.ID;
            that.TagName = item.Name;
            that.is_tag_list = false;
        },
        choose_category: function (item) {
            var that = this;
            that.CategoryID = item.ID;
            that.CategoryName = item.Name;
            that.is_category_list = false;
        },
        tel_phone: function (item) {
            api.call({
                type: 'tel_prompt',
                number: item.phone
            });
        },
        open_type_list: function () {
            var that = this;
            that.is_tag_list = false;
            that.is_category_list = false;
            that.is_type_list = !that.is_type_list;
        },
        open_tag_list: function () {
            var that = this;
            that.is_type_list = false;
            that.is_tag_list = !that.is_tag_list;
            that.is_category_list = false;
        },
        open_category_list: function () {
            var that = this;
            that.is_type_list = false;
            that.is_tag_list = false;
            that.is_category_list = !that.is_category_list;
        },
        do_choose: function (item, list) {
            item.isselected = !item.isselected;
        },
        set_select_items: function () {
            var that = this;
            // that.select_id_list = [];
            for (var i = 0; i < that.chooseList.length; i++) {
                var item = that.chooseList[i];
                if (that.ServiceType!=3) {
                    that.select_id_list.push(item);
                }
                if (that.ServiceType == 3) {
                    that.select_id_list.push(item);
                }
              
            } 
        },
        //提交
        do_save: function () {
            var that = this;
            that.set_select_items();           
            if (that.select_id_list.length == 0) {
                api.toast({
                    msg: '请选择服务人员',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if(that.chooseType=='yuangong'){
                api.sendEvent({
                    name: 'Reward_add',
                    extra: {
                        data: that.select_id_list,
                    }
                });
            }else if(that.chooseType=='yuangongchengfa'){
                api.sendEvent({
                    name: 'punishment_add',
                    extra: {
                        data: that.select_id_list,
                    }
                });
            }else{
                api.sendEvent({
                    name: 'purchase_add',
                    extra: {
                        data: that.select_id_list,
                    }
                });
            }
            
           
            setTimeout(function () {
                api.closeWin();
            }, 500)
        },
        toChooseUser: function () {
            var that = this;
            // 单选派单，重派
                that.do_save();       
        },
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    ns.init_search();
    app.ID = api.pageParam.id || 0;
    app.zxunchaID = api.pageParam.ID || 0;//装修巡查id
    app.zxtype = api.pageParam.zxtype || 0;//装修巡查类型
    app.PublishType = api.pageParam.type || 0;
    app.ServiceType = api.pageParam.ServiceType || 1;
    app.chooseType = api.pageParam.chooseType;//yuangong---惩罚奖励选择员工
    app.source = api.pageParam.source;
    app.is_xietong = api.pageParam.is_xietong || false;
    setTimeout(function () {
        app.get_data();
    }, 500)
};